package com.my.test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.my.bean.URLSAVE;
import com.my.service.CategoryService;

public class Ccs {

	/**
	 * @param args
	 */
	CategoryService categoryService;
	public static void main(String[] args) throws Exception {//   \"[\\S\\s]*\"
		
    }
	public void init(){
		List<URLSAVE> list = categoryService.selectallcontext();
		for(int i = 0;i<list.size();i++){
			URLSAVE u = list.get(i);
			String url_p = test.geturr(u.getUrl_parent());
			goall(url_p+u.getUrl_context());
		}
	}
	public void goall(String path){
		Connection connect = Jsoup.connect(path); 
		connect.header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0").timeout(13000);
        Document document;
		try {
			document = connect.get();
			String es = document.html();
			List[] arr = test.getHtmlAName(es);
			List list = arr[0];
			List<URLSAVE> list2 = arr[1];
			for (int i = 0; i < list2.size(); i++) {
				URLSAVE u = list2.get(i);
				URLSAVE urlsave = new URLSAVE(u.getUrl_name(),path,u.getUrl_context());
				try {
					URLSAVE ubool = categoryService.selectbycontext(u.getUrl_context());
				if(ubool.getUrl_name().equals("")||ubool.getUrl_parent().equals("")){
				}else{}
				} catch (NullPointerException e) {
					categoryService.addURL(urlsave);
				}
			}
			System.out.println(list.get(0));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public void ASize() throws IOException{
		Connection connect = Jsoup.connect("http://www.zhihu.com"); 
		connect.header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0").timeout(13000);
        Document document = connect.get();
        String es = document.html();
        String regex = "href=\"([^\"]+)\"";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(es);
        List l = new ArrayList();
        while (matcher.find())
        {
          l.add(matcher.group(1));
        }
        for(int i = 0;i<l.size();i++){
        	System.out.println(l.get(i));
        }
	}

}
